<html> 
<head> 
    <script src="../OLLoader.js"></script> 
    <script type="text/javascript">

    function test_initialize(t) { 
        t.plan(3); 
         
        var options = {'foo': 'bar'}; 
        var filter = new OpenLayers.Filter.Logical(options); 
        t.ok(filter instanceof OpenLayers.Filter.Logical, 
             "new OpenLayers.Filter.Logical returns object" ); 
        t.eq(filter.foo, "bar", "constructor sets options correctly"); 
        t.eq(typeof filter.evaluate, "function", "filter has an evaluate function"); 
    }

    function test_destroy(t) {
        t.plan(1);
        
        var filter = new OpenLayers.Filter.Logical();
        filter.destroy();
        t.eq(filter.filters, null, "filters array nulled properly");
    }
    
    function test_evaluate(t) {
        t.plan(1);
        
        var filter = new OpenLayers.Filter.Logical({
                type: OpenLayers.Filter.Logical.NOT});
        filter.filters.push(new OpenLayers.Filter());
        
        var feature = new OpenLayers.Feature.Vector();

        t.eq(filter.evaluate(feature.attributes), false,
                "feature evaluates to false correctly.");
    }

    function test_evaluate_feature(t) {
        t.plan(6);
        
        var feature = new OpenLayers.Feature.Vector(null, {
            pop: 200,
            name: "foo"
        });
        
        var smallPop = new OpenLayers.Filter.Comparison({
            type: OpenLayers.Filter.Comparison.LESS_THAN,
            property: "pop",
            value: 120
        });
        
        var bigPop = new OpenLayers.Filter.Comparison({
            type: OpenLayers.Filter.Comparison.GREATER_THAN,
            property: "pop",
            value: 120
        });
        
        var namedFoo = new OpenLayers.Filter.Comparison({
            type: OpenLayers.Filter.Comparison.EQUAL_TO,
            property: "name",
            value: "foo"
        });
        
        var filter;
        
        // test simple not
        filter = new OpenLayers.Filter.Logical({
            type: OpenLayers.Filter.Logical.NOT,
            filters: [smallPop]
        });
        t.eq(filter.evaluate(feature), true, "not smallPop");

        filter = new OpenLayers.Filter.Logical({
            type: OpenLayers.Filter.Logical.NOT,
            filters: [bigPop]
        });
        t.eq(filter.evaluate(feature), false, "not bigPop");

        // test or
        filter = new OpenLayers.Filter.Logical({
            type: OpenLayers.Filter.Logical.OR,
            filters: [smallPop, namedFoo]
        });
        t.eq(filter.evaluate(feature), true, "smallPop or namedFoo");

        filter = new OpenLayers.Filter.Logical({
            type: OpenLayers.Filter.Logical.OR,
            filters: [bigPop, namedFoo]
        });
        t.eq(filter.evaluate(feature), true, "bigPop or namedFoo");

        // test and
        filter = new OpenLayers.Filter.Logical({
            type: OpenLayers.Filter.Logical.AND,
            filters: [smallPop, namedFoo]
        });
        t.eq(filter.evaluate(feature), false, "smallPop and namedFoo");

        filter = new OpenLayers.Filter.Logical({
            type: OpenLayers.Filter.Logical.AND,
            filters: [bigPop, namedFoo]
        });
        t.eq(filter.evaluate(feature), true, "bigPop and namedFoo");

    }

    function test_clone(t) {
        
        t.plan(2);
        
        var filter = new OpenLayers.Filter.Logical({
            type: OpenLayers.Filter.Logical.AND,
            filters: [
                new OpenLayers.Filter.Comparison({
                    type: OpenLayers.Filter.Comparison.EQUAL_TO,
                    property: "prop1",
                    value: "val1"
                }),
                new OpenLayers.Filter.Comparison({
                    type: OpenLayers.Filter.Comparison.NOT_EQUAL_TO,
                    property: "prop2",
                    value: "val2"
                })
            ]
        });

        var clone = filter.clone();
        
        // modify the original
        filter.type = OpenLayers.Filter.Logical.OR;
        filter.filters[0].value = "nada";
        
        t.eq(clone.type, OpenLayers.Filter.Logical.AND, "clone has proper type");
        t.eq(clone.filters[0].value, "val1", "clone has cloned child filters");
        
        filter.destroy();

    }
    
    </script> 
</head> 
<body> 
</body> 
</html> 
